package cn.bdqn.veh.service.impl;

import cn.bdqn.model.EUList;
import cn.bdqn.model.PageBean;
import cn.bdqn.model.R;
import cn.bdqn.service.BaseService;
import cn.bdqn.veh.mapper.DriverMapper;
import cn.bdqn.veh.model.Driver;
import cn.bdqn.veh.model.DriverExample;
import cn.bdqn.veh.service.DriverService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author gurilla
 * @create 2019-08-18 10:25
 */
@Service
public class DriverServiceImpl extends BaseService implements DriverService {
    @Resource
    DriverMapper driverMapper;

    @Override
    public EUList<Driver> list(PageBean pageBean, String cname, String phone, String vidcard, String idcard) {
        DriverExample example = new DriverExample();
        DriverExample.Criteria criteria = example.createCriteria();
        criteria.andIsdelEqualTo(false);
        criteria.andEidEqualTo(getEid());   //从session来
        criteria.andIsstopEqualTo(false);
        if(!StringUtils.isEmpty(cname) || !StringUtils.isEmpty(phone) || !StringUtils.isEmpty(vidcard) || !StringUtils.isEmpty(idcard)){
//            criteria.andAccountEqualTo(username);
            criteria.andCnameLike("%"+cname+"%");
            criteria.andPhoneLike("%"+phone+"%");
            criteria.andVidcardLike("%"+vidcard+"%");
            criteria.andIdcardLike("%"+idcard+"%");
        }
        PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        List<Driver> drivers = driverMapper.selectByExample(example);
        PageInfo<Driver> info=new PageInfo<>(drivers);
        return new EUList<>(info.getTotal(),info.getList());
    }

    @Override
    public R save(Driver driver) {
        if(driver.getId()==null){
            driver.setEid(getEid());
            driverMapper.insertSelective(driver);
        }else{
            driverMapper.updateByPrimaryKeySelective(driver);    //更新时不更新帐号
        }
        return R.ok();
    }

    @Override
    public R delete(Integer id) {
        Driver driver = new Driver();
        driver.setId(id);
        driver.setIsdel(true);
//        try{
        driverMapper.updateByPrimaryKeySelective(driver);
        return R.ok();
//        }catch (Exception e){
//            e.printStackTrace();
//            return  R.error("删除失败");
//        }
    }

    @Override
    public R getAll() {
//        try {
        DriverExample example=new DriverExample();
        example.createCriteria().andEidEqualTo(getEid())
                .andIsdelEqualTo(false)
                .andIsadminEqualTo(false)
                .andIsstopEqualTo(false);

        List<Driver> drivers = driverMapper.selectByExample(example);
        return R.ok(null, drivers);
    }

    @Override
    public R auth(Integer id) {
        Driver driver = new Driver();
        driver.setId(id);
        driver.setIsauth(true);
        driverMapper.updateByPrimaryKeySelective(driver);
        return R.ok();
    }
}
